package com.example.consumer.service.strategy;

import com.example.common.Board;
import com.example.common.RandomUtils;

import java.util.*;

public class ColumnStrategy implements Strategy {
    @Override
    public int[] insertOneNumber(Board board, List<char[][]> allResult) {
        int[] columnCount = board.getColumnCount();
        int min = Arrays.stream(columnCount)
                .min()
                .getAsInt();
        List<Integer> minColumn = new ArrayList<>();
        for (int i = 0; i < columnCount.length; i++) {
            if (min == columnCount[i]) {
                minColumn.add(i);
            }
        }
        int minColumnIndex = minColumn.get(RandomUtils.getRandomInt(minColumn.size()));
        char[][] board1 = board.getBoard();

        Map<String, Set<Character>> map = new HashMap<>();
        for (int i = 0; i < board1.length; i++) {
            if (board1[i][minColumnIndex] == '.') {
                String s1 = i + "@" + minColumnIndex;
                map.put(s1, new HashSet<>());
            }
        }
        for (char[][] result1 : allResult) {
            for (int i = 0; i < board1.length; i++) {
                if (board1[i][minColumnIndex] == '.') {
                    String s1 = i + "@" + minColumnIndex;
                    map.get(s1).add(result1[i][minColumnIndex]);
                }
            }
        }
        return chooseFromMap(board, map);
    }
}
